package cn.com.test.common.proxy;

import cn.com.test.common.proxy.service.UserService;
import cn.com.test.common.proxy.service.UserServiceImpl;
import org.springframework.cglib.proxy.Enhancer;

import java.io.IOException;
import java.lang.reflect.Proxy;

public class Main {

	public static void main(String[] args) throws IOException {

		System.out.println("---------------JDK动态代理----------------");
		UserService userService = (UserService) Proxy.newProxyInstance(Main.class.getClassLoader(),
				new Class<?>[]{UserService.class},
				new InvocationHandlerImpl(new UserServiceImpl()));

		userService.doOption();

		System.out.println("---------------CGlib动态代理----------------");
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserServiceImpl.class);
		enhancer.setCallback(new CglibProxy());

		UserServiceImpl userService1 = (UserServiceImpl) enhancer.create();

		System.out.println(userService1.getClass().getDeclaredFields()[1].getName());

/*		byte[] generateProxyClass = ProxyGenerator.generateProxyClass("$Proxy0", new Class<?>[]{UserService.java
.class});
		FileOutputStream outputStream = new FileOutputStream
		("D:\\version\\github\\SimpleProxy\\SimpleProxy\\src\\main\\proxy\\temp\\11.class");
		outputStream.write(generateProxyClass);
		outputStream.close();*/

		userService1.doOption();
	}
}
